<?php

/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2016/3/25
 * Time: 19:31
 */
class Conversation_Model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function get_conversation_list($param){
        $sql="select * from conversation where is_deleted='N'";

        if(!empty($param['direction'])){
            $sql=$sql." AND direction='".$param['direction']."'";
        }
        if(!empty($param['server_openid'])){
            $sql=$sql." AND server_openid='".$param['server_openid']."'";
        }
        if(!empty($param['asker_openid'])){
            $sql=$sql." AND asker_openid='".$param['asker_openid']."'";
        }
        if(!empty($param['headimgurl'])){
            $sql=$sql." AND headimgurl='".$param['headimgurl']."'";
        }
        if(!empty($param['name'])){
            $sql=$sql." AND name='".$param['name']."'";
        }
        if(!empty($param['content'])){
            $sql=$sql." AND content='".$param['content']."'";
        }
        $sql=$sql." order by gmt_create ASC";
        $query=$this->db->query($sql);
        return $query->result_array();
    }

    public function get_asker_openid_list($server_openid){
        $sql="select distinct asker_openid from conversation where is_deleted='N' and server_openid='".$server_openid."'";
        $query=$this->db->query($sql);
        return $query->result_array();
    }

    public function addConversation($data){
        $query=$this->db->insert('conversation',$data);
        $affectioned=$this->db->affected_rows();
        if($affectioned==1){
            return true;
        }else{
            return false;
        }
    }
}